/*
 * @Author: your name
 * @Date: 2018-12-13 17:19:09
 * @LastEditTime: 2021-08-17 14:36:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \gdeduPlugin\js\inject.js
 */
$(function () {

});

let usedSecondes = 0;
main();

function main() {
    console.log("wellcome");
    let url = window.location.href;
    let website = "study/course";
    console.log("url", url);
    console.log("website:" + website, url.indexOf(website));
    if (url.indexOf(website) != -1) {
        console.log("进入学习页面")
        setTimeout(function () {
            patchMain();
        }, 1000);
    } else {
        console.log("不是学习页面")
    }
}

function beginPlay() {
    console.log("isFirst", isFirst, timing);
    var vdo = $("video")[0]; //jquery
    vdo.muted = true;
    player.videoPlay();

}

/**
 * 补丁主函数
 */
function patchMain() {
    list = $("input[id^='time_']");
    console.log("找到时间：" + list.length + "个");
    for (i = 0; i < list.length; i++) {
        $(list[i]).remove();
        console.log("移除时间：" + i);
    }
    //开始播放
    console.log("开始播放");
    let spanLength = $(".g-study-prompt span").length;
    console.log(" spanLength", spanLength);
    if (spanLength < 2) {
        goNext();
    } else {
        beginPlay();
    }
    setInterval(function () {
        interval();
    }, 1000);

}

/**
 * 定时程序
 */
function interval() {
    usedSecondes++;
    let checkEndTime = 10;//检测时间周期，秒
    if (usedSecondes % 56 == 0) {
        //每56秒更新1次
        updateLastViewTime(true);
    }
    if (usedSecondes % checkEndTime == 0) {
        console.log("当前已执行秒数：", usedSecondes);
        var needtime = parseInt($(".g-study-prompt span").eq(0).html());
        var lookedtime = parseInt($(".g-study-prompt span").eq(1).html());
        console.log("needtime:" + needtime, "lookedtime:" + lookedtime);
        if (lookedtime >= needtime) {
            goNext()
        }
        let spanLength = $(".g-study-prompt span").length;
        console.log(" spanLength", spanLength);
        if (spanLength < 2) {
            goNext();
        }
    }
    let needMinutes = parseInt($(".g-study-prompt span").eq(0));
    if (usedSecondes / 60 > needMinutes + 1) {
        //如果超过了需要的时间数
        window.location.reload();//刷新当前页，针对计时延时
    }
}